package com.hundsun.yr.universal.p2p.ui.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.hundsun.mobile.zwgt.R;
import com.hundsun.yr.universal.library.netstatus.NetUtils;
import com.hundsun.yr.universal.library.view.hsdialog.HSDialog;
import com.hundsun.yr.universal.p2p.ui.activity.base.BaseMineWebActivity;
import com.hundsun.yr.universal.p2p.utils.Logs;
import com.hundsun.yr.universal.p2p.utils.PermissionsUtils;


/**
 * @version V1.0
 * @Title: WebPayActivity.java
 * @Description: 支付WEB--支付页面h5
 * @date 2016-3-16 下午7:14:15
 * @author:hejun.shen
 * @email:shenhj15971@hundsun.com
 * @replace author:
 * @replace date:
 */
public class MyWebCreditActivity extends BaseMineWebActivity implements OnClickListener, HSDialog.HSDialogOnItemClickListener {
    private HSDialog dialog = null;
    private String[] permissions = new String[]{
            Manifest.permission.CALL_PHONE
    };

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    protected View getLoadingTargetView() {

        if (mBrowserLayout == null) {
        }
        return mBrowserLayout;
    }


    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {
        mBrowserLayout.refresh();
        toggleHide();
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        super.getBundleExtras(extras);
    }

    @Override
    protected void onNetworkDisConnected() {

        toggleNetworkError(true, new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                if (NetUtils.isNetworkConnected(getBaseContext())) {
                    showToast("请检查网络...");
                } else {
                    mBrowserLayout.refresh();
                    toggleHide();
                }
            }
        });

    }


    @Override
    protected void initViewsAndEvents() {
        super.initViewsAndEvents();
        toolbar_title.setText(mWebTitle);
        toolbar_menu.setText(R.string.complete);
        toolbar_menu.setTextColor(getResources().getColor(R.color.white));
        toolbar_menu.setVisibility(View.GONE);
        toolbar_menu.setOnClickListener(this);

        mBrowserLayout.mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Logs.e("电话号码" + url);
                String flagStr = getString(R.string.phone_call2);
                if (url.contains(flagStr)) {
                    dialog = new HSDialog("", getString(R.string.phone_call2),
                            getString(R.string.cancel), getString(R.string.call), mContext,
                            MyWebCreditActivity.this).setCancelable(false);//.setOnDismissListener(this);
                    dialog.show();
                } else {
                    mBrowserLayout.mWebView.loadUrl(url);
                }
                return true;
            }

            @Override
            public void onPageCommitVisible(WebView view, String url) {
                Logs.e("onPageCommitVisible0" + url);
                super.onPageCommitVisible(view, url);
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                Logs.e("url onPageStated" + url);
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                Logs.e("url onPageFinished" + url);
                super.onPageFinished(view, url);
            }
        });

    }

    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.titleBar(R.id.common_toolbar).init();
    }

    @Override
    protected boolean isApplyKitKatTranslucency() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onClick(View v) {
/*        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.toolbar_menu:

                EventBus.getDefault().post(new EventCenter<HashMap<String, String>>(Extras.INVEST_SUCCESS));
                EventBus.getDefault().post(new EventCenter<HashMap<String, String>>(Extras.FINISH));
                EventBus.getDefault().post(new EventCenter<HashMap<String, String>>(Extras.INVEST_REFRESH));
                EventBus.getDefault().post(new EventCenter<HashMap<String, String>>(Extras.ORDER_SUCCESS));

                if (NAVIGATE_TAG != null && TextUtils.equals(NAVIGATE_TAG, "TRADE")) {
                    finish();
                } else {
//				readyGoThenKill(MineInvestActivity.class);
//				readyGoThenKill(MainActivity.class);
                    finish();
                }

                break;

            default:
                break;
        }*/
    }

    @Override
    public void onItemClick(Object o, int position) {
        if ((HSDialog) o == dialog) {
            if (position != -1) {
                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    PermissionsUtils.getInstance().chekPermissions(MyWebCreditActivity.this, permissions, permissionsResult);
                    return;
                }
                startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + getString(R.string.credit_contact_way))));
            }
        }
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == event.KEYCODE_BACK) {
            if (dialog != null && dialog.isShowing()) {
                dialog.dismiss();
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    //创建监听权限的接口对象
    PermissionsUtils.IPermissionsResult permissionsResult = new PermissionsUtils.IPermissionsResult() {
        @Override
        public void passPermissons() {
//            Toast.makeText(GuideActivity.this, "权限通过，可以做其他事情!", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void forbitPermissons() {
            Toast.makeText(MyWebCreditActivity.this, "权限不通过!", Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        //就多一个参数this
        PermissionsUtils.getInstance().onRequestPermissionsResult(this, requestCode, permissions, grantResults);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + getString(R.string.credit_contact_way))));
    }
}
